$(function () {
  var queryDict = {};
  location.search
    .substr(1)
    .split('&')
    .forEach(function (item) {
      queryDict[item.split('=')[0]] = item.split('=')[1];
    });

  if (queryDict.am) {
    setCookie('am', queryDict.am);
  }

  var partner = false;
  if (queryDict.admitad_uid) {
    partner = 'admitad';
  }
  if (queryDict.subid) {
    partner = 'wakeapp';
  }
  if (queryDict.lttracking) {
    partner = 'leadtrade';
  }
  if (queryDict.ad) {
    var isYandexMoneyPartner = /^yandex-money/.test(queryDict.ad);
    partner = isYandexMoneyPartner ? 'yandex-money' : queryDict.ad;
  }
  if(queryDict.advertiser_id===1){
  	partner="affise";
  }
  if (!partner) {
    return;
  }

  var date = new Date(
    new Date().getTime() + 30 * 24 * 60 * 60 * 1000
  ).toUTCString();

  var supported = [
    { pattern: /^admitad$/, uuidField: 'admitad_uid' },
    { pattern: /^advertise$/, uuidField: 'uid' },
    { pattern: /^adsbalance$/, uuidField: 'id' },
    { pattern: /^cityads$/, uuidField: 'click_id' },
    { pattern: /^rocket10$/, uuidField: 'click_id' },
    { pattern: /^alfaleads$/, uuidField: 'clickid' },
    { pattern: /^3snet$/, uuidField: 'click_id' },
    { pattern: /^777$/, uuidField: 'click_id' },
    { pattern: /^offergate10$/, uuidField: 'click_id' },
    { pattern: /^actionpay$/, uuidField: 'actionpay' },
    { pattern: /^justlink$/, uuidField: 'click_id' },
    { pattern: /^directadvert$/, uuidField: 'click_id' },
    { pattern: /^marketgid$/, uuidField: 'click_id' },
    { pattern: /^omnimix$/, uuidField: 'click_id' },
    { pattern: /^smarttraf$/, uuidField: 'click_id' },
    { pattern: /^wakeapp$/, uuidField: 'subid' },
    { pattern: /^yandex-money$/, uuidField: 'clickId' },
    { pattern: /^leadtrade/, uuidField: 'click_id' },
    { pattern: /^affise/, uuidField: 'click_id' },
    { pattern: /^brlab/, uuidField: 'click_id' },
  ];

  var adCookieParam = partner;
  if (partner == '777') {
    adCookieParam = 'CpaKing';
  } else if (partner == 'offergate10') {
    adCookieParam = 'offergate';
  }
  var utmSource = queryDict.utm_source ? queryDict.utm_source : '';
  var utmMedium = queryDict.utm_medium ? queryDict.utm_medium : '';
  var utmCampaign = queryDict.utm_campaign ? queryDict.utm_campaign : '';
  var utmContent = queryDict.utm_content ? queryDict.utm_content : '';
  var utmTerm = queryDict.utm_term ? queryDict.utm_term : '';
  var adCookieValue =
    adCookieParam +
    '###' +
    utmSource +
    '###' +
    utmMedium +
    '###' +
    utmCampaign +
    '###' +
    utmContent +
    '###' +
    utmTerm;
  setAdCookie('ad', adCookieValue);

  if (isSupported(partner)) {
    var uid = queryDict[getUidField(partner)];
    var cookieParam = partner;
    if (partner == '777') {
      cookieParam = 'CpaKing';
    } else if (partner == 'offergate10') {
      cookieParam = 'offergate';
    }
    if (uid === undefined) {
      uid = Math.floor(100000000 + Math.random() * 900000000);
    }
    value = cookieParam + '###' + uid;
    if (queryDict.utm_campaign) {
      value += '###' + queryDict.utm_campaign;
      if (queryDict.utm_content) {
        value += '###' + queryDict.utm_content;
      }
    }

    setCookie('partner', value);
  } else if(partner==="affise"){
    let value=partner+"###"+queryDict.sub1+"###"+queryDict.pid+"###"+queryDict.offer_id;
    setCookie("partner", value);
  } else if (partner === '1may-spec' && queryDict.ph) {
    setCookie('1may-spec', queryDict.ph, 10);
  } else {
    removeCookie('partner');
    var value = partner;
    if (queryDict.utm_campaign) {
      value += '#' + queryDict.utm_campaign;
      if (queryDict.utm_content) {
        value += '#' + queryDict.utm_content;
      }

      setCookie('ad_log', value);
    }
  }

  function isSupported(partner) {
    var result = getPartnerInfo(partner);
    if (result) {
      return true;
    }
    return false;
  }

  function getUidField(partner) {
    var result = getPartnerInfo(partner);
    if (result) {
      return result.uuidField;
    }

    return null;
  }

  function getPartnerInfo(partner) {
    var result = supported.filter(function (partnerInfo) {
      return partnerInfo.pattern.test(partner);
    });

    if (result.length > 0) {
      return result[0];
    }

    return null;
  }

  function getCookie(name) {
    var matches = document.cookie.match(
      new RegExp(
        '(?:^|; )' +
          name.replace(/([\.$?*|{}\(\)\[\]\\\/\+^])/g, '\\$1') +
          '=([^;]*)'
      )
    );
    return matches ? decodeURIComponent(matches[1]) : null;
  }

  function removeCookie(name) {
    document.cookie = name + '=123;expires=Thu, 01 Jan 1970 00:00:01 GMT';
  }

  function setCookie(name, value, days) {
    var expires = new Date(
      new Date().getTime() + (days || 30) * 24 * 60 * 60 * 1000
    ).toUTCString();
    document.cookie =
      name +
      '=' +
      value +
      '; expires=' +
      expires +
      '; path=/; ' +
      '; domain=' +
      getDomain();
  }

  function setAdCookie(name, value) {
    var expires = new Date(new Date().getTime() + 30 * 60 * 1000).toUTCString();
    document.cookie =
      name +
      '=' +
      value +
      '; expires=' +
      expires +
      '; path=/; ' +
      '; domain=' +
      getDomain();
  }

  function getDomain() {
    return (
      '.' + document.location.hostname.replace('www.', '').replace('m.', '')
    );
  }
});
